//
//  WaterFulViewController.m
//  瀑布流
//
//  Created by 花花蔡 on 2017/3/1.
//  Copyright © 2017年 zws. All rights reserved.
//

#import "WaterFulViewController.h"
#import "WSCollectionCell.h"
#import "WSLayout.h"
#import "CellModel.h"
#import "SJAvatarBrowser.h"
@interface WaterFulViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) WSLayout *layout;


@end

@implementation WaterFulViewController {
    
    NSMutableArray *_dataArray;
    int _index;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"福利壁纸";
    self.view.backgroundColor = iCodeTableviewBgColor;
    _index = 1;
    [self setupUI];

    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)loadData {
    
    __weak typeof(self) weakSelf = self;
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSString *index = [NSString stringWithFormat:@"%d",_index];
        NSString *url = [NSString stringWithFormat:@"%@%@%@",FuLiBiZhiUrlStr_1,index,FuLiBiZhiUrlStr_2];
        [[NetworkTools shareTools] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"%@",responseObject);
            NSDictionary *data = responseObject[@"data"];
            NSArray *WallpaperListInfo = data[@"WallpaperListInfo"];
            _dataArray = [CellModel mj_objectArrayWithKeyValuesArray:WallpaperListInfo];
            
            [weakSelf.collectionView reloadData];
            [weakSelf.collectionView.mj_header endRefreshing];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [weakSelf.collectionView.mj_header endRefreshing];
        }];
        
    }];

    [self.collectionView.mj_header beginRefreshing];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _index += 1;
        NSString *index = [NSString stringWithFormat:@"%d",_index];
        NSString *url = [NSString stringWithFormat:@"%@%@%@",FuLiBiZhiUrlStr_1,index,FuLiBiZhiUrlStr_2];
        [[NetworkTools shareTools] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"%@",responseObject);
            NSDictionary *data = responseObject[@"data"];
            NSArray *WallpaperListInfo = data[@"WallpaperListInfo"];
            WallpaperListInfo = [CellModel mj_objectArrayWithKeyValuesArray:WallpaperListInfo];
            for (CellModel *model in WallpaperListInfo) {
                [_dataArray addObject:model];
            }
            [weakSelf.collectionView reloadData];
            [weakSelf.collectionView.mj_footer endRefreshing];
            
            if (WallpaperListInfo.count == 0) {
                [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    weakSelf.collectionView.mj_footer.hidden = YES;
                });
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [weakSelf.collectionView.mj_footer endRefreshing];
        }];

    }];
}

- (void)setupUI {
    
    self.layout = [[WSLayout alloc] init];
    self.layout.lineNumber = 2; //列数
    self.layout.rowSpacing = AAdaption(5/AAdaptionWidth()); //行间距
    self.layout.lineSpacing = AAdaption(5/AAdaptionWidth()); //列间距
    self.layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kheight) collectionViewLayout:self.layout];
    self.edgesForExtendedLayout = UIRectEdgeNone;

    [self.collectionView registerClass:[WSCollectionCell class] forCellWithReuseIdentifier:@"collectionCell"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = iCodeTableviewBgColor;
    
    [self.view addSubview:self.collectionView];
    
    
    //返回每个cell的高   对应indexPath
    [self.layout computeIndexCellHeightWithWidthBlock:^CGFloat(NSIndexPath *indexPath, CGFloat width) {
        
        CGFloat newHeight = AAdaption(200/AAdaptionWidth());
        return newHeight;
    }];

}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WSCollectionCell *cell = (WSCollectionCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    
    cell.model = _dataArray[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"选中了第%ld个item",indexPath.row);
    CellModel *model = _dataArray[indexPath.row];
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.WallPaperBig]];
    
    UIImage *image = [UIImage imageWithData:data];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [SJAvatarBrowser showImage:imageView];
}


@end
